package com.ly.mp.dal.comm.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
@ConfigurationProperties(prefix = "other.write.mp.jdbc", ignoreUnknownFields = true)
public class DatabaseSet {
    private static final String DBTYPE_ORACLE = "oracle";
    private static final String DBTYPE_MYSQL = "mysql";
    private static final String DBTYPE_SQLSERVER = "sqlserver";
    private List<String> name;
    private List<String> url;
    private List<String> username;
    private List<String> password;
    private List<DataSourceInfo> dataSourceInfo = new ArrayList<>();
    private Map<String, String> dbTypeMap = new HashMap<>();
    private String default_key = "default";
    private String originalDefaultName;
    @Autowired
    private DataSourceInfo dbInfo;
    @Autowired
    private DataBaseReadSet dbReadSet;

    public String getDefaultKey() {
        return this.default_key + "_writeDBKey";
    }

    public String getOriginalDefaultName() {
        return this.originalDefaultName;
    }

    public void setOriginalDefaultName(String originalDefaultName) {
        this.originalDefaultName = originalDefaultName;
    }

    @PostConstruct
    public void init() {
        this.originalDefaultName = this.dbInfo.getName();
        this.dbInfo.setName(this.default_key);
        this.dbInfo.setOrigName(this.originalDefaultName);
        this.dataSourceInfo.add(this.dbInfo);
        this.dbTypeMap.put(this.dbInfo.getName() + "_writeDBKey", this.getDBType(this.dbInfo));
        if (this.name != null && !this.name.isEmpty()) {
            int dbsize = this.name.size();

            for (int i = 1; i < dbsize; ++i) {
                DataSourceInfo info = new DataSourceInfo();
                info.setOrigName((String) this.name.get(i));
                info.setName((String) this.name.get(i));
                info.setUrl((String) this.url.get(i));
                info.setUsername((String) this.username.get(i));
                info.setPassword((String) this.password.get(i));
                info.setTransactionPolicy(this.dbInfo.getTransactionPolicy());
                this.dataSourceInfo.add(info);
                this.dbTypeMap.put(this.dbInfo.getName() + "_writeDBKey", this.getDBType(info));
            }

        }
    }

    private String getDBType(DataSourceInfo info) {
        String dbType = "oracle";
        if (this.dbInfo.getUrl().contains("mysql")) {
            dbType = "mysql";
        } else if (this.dbInfo.getUrl().contains("oracle")) {
            dbType = "oracle";
        } else {
            dbType = "sqlserver";
        }

        return dbType;
    }

    public String getDBTypeMap(String dskey) {
        if (dskey == null) {
            this.dbTypeMap.get(this.getDefaultKey());
        }

        return (String) this.dbTypeMap.get(dskey);
    }

    public List<DataSourceInfo> getDataSourceInfo() {
        return this.dataSourceInfo;
    }

    public List<String> getName() {
        return this.name;
    }

    public List<String> getUrl() {
        return this.url;
    }

    public List<String> getUsername() {
        return this.username;
    }

    public List<String> getPassword() {
        return this.password;
    }

    public void setName(List<String> name) {
        name.add(0, "default");
        this.name = name;
    }

    public void setUrl(List<String> url) {
        url.add(0, "default");
        this.url = url;
    }

    public void setUsername(List<String> username) {
        username.add(0, "default");
        this.username = username;
    }

    public void setPassword(List<String> password) {
        password.add(0, "default");
        this.password = password;
    }
}